//存放主要交互逻辑js代码
//JavaScript 模块化
var seckill = {
		//封装秒杀相关ajax的url
		URL:{
			now : function(){
				return '/Demo/seckill/time/now';
			},

			exposer : function(seckillId){
				return '/Demo/seckill/'+seckillId+'/exposer';
			},
			
			execution : function(seckillId,md5){
				return '/Demo/seckill/'+seckillId+'/'+md5+'/excution';
			}
		},
		handleSeckillkill : function(seckillId,node){
			//获取秒杀地址，控制显示逻辑,处理秒杀逻辑
			node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">秒杀</button>');
			$.post(seckill.URL.exposer(seckillId),{},function(result){
				//在回调函数中，执行交互流程
				if(result && result['success']){
					var exposer = result['data'];
					if(exposer['exposed']){
						//开启秒杀
						//获取秒杀地址
						var md5 = exposer['md5'];
						var killUrl = seckill.URL.execution(seckillId, md5);
						console.log("killUrl:"+killUrl);
						
						$('#killBtn').one('click',function(){
							//执行秒杀请求
							//1:先禁用按钮
							$(this).addClass('disabled');
							//2：发送秒杀其请求
							$.post(killUrl,{},function(result){
								console.log(result);
								if(result && result['success']){
									var killResult = result['data'];
									var state = killResult['state'];
									var statInfo = killResult['statInfo'];
									//3:显示秒杀结果
									node.html('<span class="label-success">' +statInfo+ '</spam>');
								}
							});
						});
						node.show();
					}else{
						//未开启秒杀
						var now = exposer['now'];
						var start = exposer['start'];
						var end = exposer['end'];
						//重新计算及时逻辑
						seckill.countdown(seckillId,now,start,end);
						
					}
				}else{
					console.log("result:"+result);
				}
			});
		},
		
		
		//验证手机号
		validatePhone : function(phone){
			if(phone && phone.length == 11 && !isNaN(phone)){
				return true;
			}else{
				return false;
			}
		},
		countdown : function(seckillId,nowTime,startTime,endTime){
			var seckillBox = $('#seckill-box');
			//时间判断
			if(nowTime > endTime){
				//秒杀结束
				seckillBox.html('秒杀结束');
			}else if(nowTime < startTime){
				//秒杀未开始
				var killTime = new Date(startTime + 1000);
				seckillBox.countdown(killTime,function(event){
					//时间格式
					var format = event.strftime('秒杀倒计时: %D天 %H时 %M分 %S秒 ');
					seckillBox.html(format);
					//时间完成后回调事件
				}).on('finish.countdown',function(){
					//要获取秒杀地址，控制显示逻辑，执行秒杀按钮
					seckill.handleSeckillkill(seckillId, seckillBox);
				});
			}else{
				//秒杀开始
				seckill.handleSeckillkill(seckillId,seckillBox);
			}
		},
		//详情页秒杀罗技
		detail:{
			//详情页初始化
			init : function(params){
				//手机验证和登录，计时交互
				//在cookie中查询手机号
				var killPhone = $.cookie('killPhone');
				//验证手机号
				if(!seckill.validatePhone(killPhone)){
					//绑定手机号
					var killPhoneModal = $("#killPhoneModal");
					killPhoneModal.modal({
						show : true,
						backdrop : 'static',
						keyboard : false
					});
					$("#killPhoneBtn").click(function(){
						var inputPhone = $('#killPhonekey').val(); 
						if(seckill.validatePhone(inputPhone)){
							//电话写入cookie
							$.cookie('killPhone', inputPhone, {expires:7, path:'/Demo/seckill'});
							window.location.reload();
						}else{
							$('#killPhoneMessage').hide().html('<label class="label label-danger">手机号错误!</label>').show(300);
						}
					});
				}
				//已经登录
				var startTime = params['startTime'];
				var endTime = params['endTime'];
				var seckillId = params['seckillId'];
				$.get(seckill.URL.now(),{},function(result){
					if(result && result['success']){
						var nowTime = result['data'];
						//时间判断
						seckill.countdown(seckillId,nowTime,startTime,endTime);
					}else{
						console.log('result:'+result);
					}
				});
			}
		}
		
}